package com.zxy.demo.guava.beanutils;

import org.springframework.cglib.beans.BeanCopier;

/**
 * Main.
 *
 * Created by zxybird on 17/1/24.
 */
public class Main {

  public static void main(String[] args) {
    FromBean fb = new FromBean();
    fb.setAddress("北京市朝阳区大屯路");
    fb.setAge(20);
    fb.setMoney(30000.111);
    fb.setIdno("110330219879208733");
    fb.setName("测试");

    IMethodCallBack apacheBeanUtilCB = new IMethodCallBack() {

      @Override
      public String getMethodName() {
        return "org.apache.commons.beanutils.BeanUtil.copyProperties";
      }

      @Override
      public ToBean callMethod(FromBean frombean) throws Exception {

        ToBean toBean = new ToBean();
        org.apache.commons.beanutils.BeanUtils.copyProperties(toBean, frombean);
        return toBean;
      }
    };

    IMethodCallBack apachePropertyUtilsCB = new IMethodCallBack() {

      @Override
      public String getMethodName() {
        return "org.apache.commons.beanutils.PropertyUtils.copyProperties";
      }

      @Override
      public ToBean callMethod(FromBean frombean) throws Exception {
        ToBean toBean = new ToBean();
        org.apache.commons.beanutils.PropertyUtils.copyProperties(toBean, frombean);
        return toBean;
      }
    };

    IMethodCallBack springBeanUtilsCB = new IMethodCallBack() {

      @Override
      public String getMethodName() {
        return "org.springframework.beans.BeanUtils.copyProperties";
      }

      @Override
      public ToBean callMethod(FromBean frombean) throws Exception {
        ToBean toBean = new ToBean();
        org.springframework.beans.BeanUtils.copyProperties(frombean, toBean);
        return toBean;
      }
    };

    IMethodCallBack cglibCB = new IMethodCallBack() {
      BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class, false);

      @Override
      public String getMethodName() {
        return "BeanCopier.create";
      }

      @Override
      public ToBean callMethod(FromBean frombean) throws Exception {
        ToBean toBean = new ToBean();
        bc.copy(frombean, toBean, null);
        return toBean;
      }
    };

    // 数量较少的时候，测试性能
    BenchmarkTest bt = new BenchmarkTest(10);
    bt.benchmark(apacheBeanUtilCB, fb);
    bt.benchmark(apachePropertyUtilsCB, fb);
    bt.benchmark(springBeanUtilsCB, fb);
    bt.benchmark(cglibCB, fb);

    // 测试一万次性能测试
    BenchmarkTest bt10000 = new BenchmarkTest(10000);
    bt10000.benchmark(apacheBeanUtilCB, fb);
    bt10000.benchmark(apachePropertyUtilsCB, fb);
    bt10000.benchmark(springBeanUtilsCB, fb);
    bt10000.benchmark(cglibCB, fb);

    // 担心因为顺序问题影响测试结果
    BenchmarkTest bt1000R = new BenchmarkTest(10000);
    bt1000R.benchmark(cglibCB, fb);
    bt1000R.benchmark(springBeanUtilsCB, fb);
    bt1000R.benchmark(apachePropertyUtilsCB, fb);
    bt1000R.benchmark(apacheBeanUtilCB, fb);

  }

}
